package vn.com.demand.lux.preferences;

import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import vn.com.demand.lux.Activator;

import com.swtdesigner.FieldLayoutPreferencePage;

public class XMLLocationPreference extends FieldLayoutPreferencePage implements
		IWorkbenchPreferencePage {

	private DirectoryFieldEditor xmlFolderLocation;
	/**
	 * Create the preference page
	 */
	public XMLLocationPreference() {
		super();
		setTitle("XML Folder Location");
		setDescription("Select folder location");
	}

	/**
	 * Create contents of the preference page
	 * @param parent
	 */
	@Override
	public Control createPageContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final FormData fd_container = new FormData();
		fd_container.top = new FormAttachment(0, 35);
		fd_container.left = new FormAttachment(0, 55);
		container.setLayoutData(fd_container);
		container.setLayout(new FormLayout());

		final Group accountReceivableGroup = new Group(container, SWT.NONE);
		accountReceivableGroup.setText("Location");
		final FormData fd_accountReceivableGroup = new FormData();
		fd_accountReceivableGroup.right = new FormAttachment(100, -11);
		fd_accountReceivableGroup.bottom = new FormAttachment(0, 120);
		fd_accountReceivableGroup.top = new FormAttachment(0, 15);
		fd_accountReceivableGroup.left = new FormAttachment(0, 5);
		accountReceivableGroup.setLayoutData(fd_accountReceivableGroup);
		accountReceivableGroup.setLayout(new FormLayout());

		final Composite composite = new Composite(accountReceivableGroup, SWT.NONE);
		final FormData fd_composite = new FormData();
		fd_composite.right = new FormAttachment(100, -2);
		fd_composite.top = new FormAttachment(0, 30);
		fd_composite.left = new FormAttachment(0, 5);
		composite.setLayoutData(fd_composite);

		xmlFolderLocation = new DirectoryFieldEditor(PreferenceConstants.XML_ACCOUNTRECEIVABLE, "Folder", composite);
		addField(xmlFolderLocation);

		return container;
	}

	/**
	 * Initialize the preference page
	 */
	public void init(IWorkbench workbench) {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	@Override
	protected void performDefaults() {
		super.performDefaults();
	}

	@Override
	public boolean performOk() {
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.XML_ACCOUNTRECEIVABLE, xmlFolderLocation.getStringValue());
		return super.performOk();
	}

	@Override
	protected void performApply() {
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.XML_ACCOUNTRECEIVABLE, xmlFolderLocation.getStringValue());
		super.performApply();
	}

}
